/**
 *
 */
package com.alibaba.middleware.hsf.guide.client.normal;

import com.alibaba.middleware.hsf.guide.api.common.StopGuard;
import com.alibaba.middleware.hsf.guide.api.model.OrderModel;
import com.alibaba.middleware.hsf.guide.api.service.OrderService;

import com.taobao.hsf.app.api.util.HSFApiConsumerBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weipeng2k 2016年2月19日 下午5:18:10
 */
public class Start {

    public static void main(String[] args) {
        System.setProperty("project.name", "HSF-Guide-Client-Normal");
        System.setProperty("hsf.run.mode", "0");
        HSFApiConsumerBean hsfApiConsumerBean = new HSFApiConsumerBean();
        hsfApiConsumerBean.setInterfaceName("com.alibaba.middleware.hsf.guide.api.service.OrderService");
        hsfApiConsumerBean.setVersion("1.0.0");
        hsfApiConsumerBean.setGroup("HSF");
        hsfApiConsumerBean.setClientTimeout(100);
        try {
            hsfApiConsumerBean.init(10000);
            final OrderService orderService = (OrderService) hsfApiConsumerBean.getObject();
            System.out.println("normal client start.");

            StopGuard.run(new Runnable() {
                @Override
                public void run() {
                    List<Long> idS = new ArrayList<Long>();
                    for (long i = 1; i <= 10; i++) {
                        idS.add(i);
                    }
                    List<OrderModel> orderModels = orderService.queryOrderList(idS);
                    System.out.println("order 1..10 sumPayment is : " + sumPayment(orderModels));
                }
            });


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 统计订单的支付总额
     *
     * @param orderModelList 订单列表
     * @return 支付总额
     */
    private static long sumPayment(List<OrderModel> orderModelList) {
        long sumPayment = 0;
        if (orderModelList != null) {
            for (OrderModel orderModel : orderModelList) {
                if (orderModel.getTotalPayment() != null) {
                    sumPayment += orderModel.getTotalPayment();
                }
            }
        }

        return sumPayment;
    }
}
